	package com.amado.easyui2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.amado.easyui2.constant.GlobalConstants;
import com.amado.easyui2.domain.CoachStaffEntity;
import com.amado.easyui2.exception.ApiException;
import com.amado.easyui2.service.CoachStaffService;
import com.amado.easyui2.util.JsonUtil;
import com.amado.easyui2.util.SessionUtils;
import com.amado.easyui2.vo.Page;
import com.amado.easyui2.vo.Result;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CoachStaffServlet extends HttpServlet {
//	private CoachStaffService coachStaffService = new CoachStaffServiceImpl();
//	private static ObjectMapper mapper = new ObjectMapper();
//	private static final long serialVersionUID = 1L;
	CoachStaffService  coachStaffService;
	private static final long serialVersionUID = 1L;
	private static ObjectMapper mapper = new ObjectMapper();
//    private DriverManagerDataSource dataSource;
	ApplicationContext applicationContext;
	@Override
	public void init() throws ServletException {
		//Spring容器
		applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
				//通过容器获取Bean
		coachStaffService = applicationContext.getBean("coachStaffServiceImpl",CoachStaffService.class);
	}
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		arg0.setCharacterEncoding("UTF-8");
		arg1.setCharacterEncoding("UTF-8");
		String method=arg0.getParameter("method");
		if("add".equals(method)) {
			add(arg0,arg1);
		}else if("update".equals(method)) {
			update(arg0,arg1);
		}else if("edit".equals(method)) {
			edit(arg0,arg1);
		}else if("delete".equals(method)) {
			delete(arg0,arg1);
		}else if("query".equals(method)) {
			query(arg0,arg1);
		}
	}

	private void add(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		try {
		String strJson = arg0.getParameter("data");
		CoachStaffEntity coachStaffInfo = JsonUtil.jsonToBean(strJson, CoachStaffEntity.class);
		String coachNa = SessionUtils.getUserName(arg0);
		coachStaffInfo.setCreateName(coachNa);
		coachStaffService.add(coachStaffInfo);
		}catch (ApiException e) {
			arg1.getWriter().print(JsonUtil.beanToJson(new Result(e.getCode(),e.getMessage())));
		}
	}
	private void update(HttpServletRequest arg0, HttpServletResponse arg1) {
		String strJson = arg0.getParameter("data");
		CoachStaffEntity coachInfo = JsonUtil.jsonToBean(strJson, CoachStaffEntity.class);
		coachInfo.setCreateName(SessionUtils.getUserName(arg0));
		coachStaffService.update(coachInfo);
	}

	private void edit(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		Long id = Long.valueOf(arg0.getParameter("id"));
		PrintWriter out = arg1.getWriter();
		out.print(mapper.writeValueAsString(coachStaffService.getCoach(id)));
	}

	private void delete(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
//		String delete = arg0.getParameter("delete");
//		if (delete != null && delete.trim() != "") {
//			coachStaffService.delete(Long.valueOf(delete));
//			arg1.getWriter().print(JsonUtil.beanToJson(new Result()));
//		} else {
//			arg1.getWriter().print(JsonUtil.beanToJson(new Result("0", "参数错误")));
//		}
		PrintWriter out=arg1.getWriter();
		try {
			String ids = arg0.getParameter("ids");
			if (ids != null) {
				String[] idAray = ids.split(",");
				if (idAray.length > 5) {
					out.print(mapper.writeValueAsString(new Result("2", "删除数据不能超过5个")));
				} else {
					List<Long> idList = new ArrayList<>();
					for (String tempId : idAray) {
						// idList.add(Long.valueOf(Long.parseLong(tempId)));
						// idList.add(Long.parseLong(tempId));
						idList.add(Long.valueOf(tempId));
					}
					coachStaffService.delete(idList);
					// 1:成功
					out.print(mapper.writeValueAsString(new Result()));
				}
			}
		} catch (Exception e) {
			out.print(mapper.writeValueAsString(new Result("2", e.getMessage())));
		}
		
	}


	private void query(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		Page page = new Page();
		String strPage = arg0.getParameter("page");
		String strRows = arg0.getParameter("rows");
		int page1= strPage==null?1:Integer.parseInt(strPage);//
		int rows = strRows==null?5:Integer.parseInt(strRows);
		String cn = arg0.getParameter("cn");
		String cIC = arg0.getParameter("cIC");
		page.setCurrentPage(page1);
		page.setRowSize(rows);
		Page<CoachStaffEntity> pageList = coachStaffService.listAll(cn, cIC, page);
		PrintWriter out = arg1.getWriter();
		out.print(mapper.writeValueAsString(pageList));
		
	}

}
